﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;

using com.tiyma.common;
using com.tiyma.components;
using com.tiyma.model;

namespace com.tiyma.web
{
    public partial class Index : BasePage
    {
        private string m_nav = "<a  href=\"/user/Reg.aspx\">注册</a>|<a  href=\"/user/Login.aspx\">登录</a>";

        private const string m_last_update_template="<ul><li class=\"ro0\">[<a href=\"/bookList.aspx?k={0}&t=3\" target=\"_blank\">{1}</a>]</li><li class=\"ro1\"><a href=\"/book/{2}/\" class=\"f14\" target=\"_blank\">{3}</a></li><li class=\"ro2\"><a href=\"/{9}/{8}/{4}/\" target=\"_blank\">{5}</a></li><li class=\"ro3\">{6}</li><li class=\"ro4\">{7}</li></ul>";

        private const string m_top_pic_recommend_book = "<div class=\"book_Recommended\"><div class=\"book_Recommended_img\"> <a href=\"/book/{0}/\" target=\"_blank\"><img src=\"{1}\" /></a></div><div class=\"book_Recommended_text\"> <h1><a href=\"/book/{0}/\" target=\"_blank\">{2}</a></h1><div class=\"author\">作者：{3}</div><div class=\"Intro\"> {4}</div></div></div>";

        private const string m_bottom_pic_recommend_book = "<div class=\"comment\"> <div class=\"imgtxt\"><a href=\"/book/{0}/\" class=\"pic\" target=\"_blank\"><img src=\"{1}\" /></a><h3><a href=\"/book/{0}/\" target=\"_blank\" >{2}</a></h3><ul><li>作者:<span>{3}</span></li><li>类别:<span>{4}</span></li><li>状态:<span>{5}</span></li><li>时间:<span>{6}</span></li><li><a href=\"/book/{0}/\" class=\"more\">查看详情&gt;&gt;</a></li></ul></div> </div>";

        private const string m_right_recommend_book = "<li {3}><em>{0}</em><a href=\"/book/{1}/\" target=\"_blank\">{2}</a></li>";

        private const string m_cur_read_template = "<div class=\"wrap\"><div class=\"curRead\"><span class=\"bt\">最近阅读</span> {0}</div></div>";

        public string m_cur_read_list = string.Empty;

        BookManager m_book_mgr = new BookManager();

        List<BookInfo> m_list = null;

        protected void Page_Load(object sender, EventArgs e)
        {
          
          

            if (Token != null)
            {
                lblNav.Text = string.Format("{0}|<a href=\"/user/index.aspx\" target=\"_blank\">我的信息</a>|<a href=\"/user/bookcase.aspx\" target=\"_blank\">我的书架</a>|<a href=\"/Out.aspx?retUrl={1}\">退出</a>", !string.IsNullOrEmpty(Token.NickName) ? Token.NickName : Token.UserName, Request.Url.ToString());
            }
            else
            {
                lblNav.Text = m_nav;
            }

            m_list = m_book_mgr.GetDefaultRecommendBook();


            BindLastUpdate();

            BindPicRecommendBook();

            BingRightRecommend();

            BindCurRead();
        }

        /// <summary>
        /// 最近阅读
        /// </summary>
        private void BindCurRead()
        {
            List<ChapterInfo> t_list = CookieManager.GetCurReadList();
           
            if (t_list != null && t_list.Count > 0)
            {
                StringBuilder t_sb = new StringBuilder();
                int index = 0;
                foreach (ChapterInfo t_chapter in t_list)
                {
                    t_sb.AppendFormat("<a href=\"/book/{0}/\" target=\"_target\" class=\"bk\">{1}</a>[<a href=\"/{5}/{2}/{3}/\" target=\"_target\" class=\"ck\">{4}</a>]", t_chapter.BookId, t_chapter.BookName,t_chapter.SiteId, t_chapter.ChapterId, t_chapter.ChapterName.Trim(),t_chapter.IsDownLoad==Consts.ExistChapterContent?"reader":"chapter");
                    index++;
                    if (index == 4)
                        break;
                }

                m_cur_read_list = string.Format(m_cur_read_template, t_sb.ToString());
            }
        }

        /// <summary>
        /// 绑定右边推荐
        /// </summary>
        private void BingRightRecommend()
        {
            StringBuilder t_sb = new StringBuilder();

            try
            {
             
                int index = 1;

                t_sb.Append("<ol class=\"ranklist ranklist_txt\">");
                if (m_list != null && m_list.Count > 7)
                {
                    List<BookInfo> t_list = m_list.GetRange(7, (m_list.Count - 7) > 20 ? 20 : (m_list.Count - 7));
                    foreach (BookInfo t_book in t_list)
                    {
                        t_sb.AppendFormat(m_right_recommend_book
                            , index
                            , t_book.BookId
                            , t_book.BookName
                            ,(index==1||index==2|| index==3 || index==11|| index==12 || index==13)?"class=\"top\"":""

                           );
                        index++;
                        if (index == 11)
                        {
                            t_sb.Append("</ol><ol class=\"ranklist ranklist_txt\">");
                           
                        }
                    }
                }

                lblRightRecommend.Text = t_sb.ToString();
            }
            catch (Exception ex)
            {
                LogHelper.Error("绑定右边推荐失败" + ex.ToString());
                RedirectErrorPage();
            }
        }

        /// <summary>
        /// 绑定封面推荐小说
        /// </summary>
        private void BindPicRecommendBook()
        {
            StringBuilder t_sb = new StringBuilder();

            StringBuilder t_sb_bottom = new StringBuilder();
            
            try
            {
                if (m_list != null && m_list.Count > 0)
                {
                    List<BookInfo> t_list = m_list.GetRange(0, m_list.Count > 7 ? 7 : m_list.Count);
                    int index = 0;
                    if (t_list != null && t_list.Count > 0)
                    {
                        foreach (BookInfo t_book in t_list)
                        {

                            if (index < 4)
                            {
                                t_sb.AppendFormat(m_top_pic_recommend_book
                                    , t_book.BookId
                                    , t_book.ICONUrl
                                    , t_book.BookName
                                    , t_book.AuthorName
                                    ,string.IsNullOrEmpty( SystemUtil.GetGBString(t_book.Description, 100))?string.Format("一本{0}类型的佳作，在本站已经具有不少的点击，占在首页这个位置也是读者的推介",t_book.BookCategory):SystemUtil.GetGBString(t_book.Description, 100)
                                   );
                            }
                            else
                            {
                                t_sb_bottom.AppendFormat(m_bottom_pic_recommend_book
                                     , t_book.BookId
                                     , t_book.ICONUrl
                                     , t_book.BookName
                                     , t_book.AuthorName
                                     , t_book.BookCategory
                                     , (t_book.LastUpdateChapterName.IndexOf("完") > -1 || t_book.LastUpdateChapterName.IndexOf("终") > -1 || t_book.LastUpdateChapterName.IndexOf("新书") > -1 || DateTime.Now.Subtract(t_book.LastUpdateTime).Days > 2) ? "完结" : "连载"
                                     , t_book.LastUpdateTime.ToString("yyyy-MM-dd"));
                            }
                            index++;
                            if (index == 7)
                                break;
                        }
                    }
                }
                lblBottomRecommend.Text = t_sb_bottom.ToString();
                lblTopRecommend.Text = t_sb.ToString();
            }
            catch (Exception ex)
            {
                LogHelper.Error("绑定小说最新更新失败" + ex.ToString());
                RedirectErrorPage();
            }
        }

        /// <summary>
        /// 绑定最近更新
        /// </summary>
        private void BindLastUpdate()
        {
            StringBuilder t_sb = new StringBuilder();
          
            try
            {
                List<BookInfo> t_list = m_book_mgr.GetLastUpdateBook();

                if (t_list != null && t_list.Count > 0)
                {
                    foreach (BookInfo t_book in t_list)
                    {
                        t_sb.AppendFormat(m_last_update_template
                            , HttpUtility.UrlEncode(t_book.BookCategory,Encoding.GetEncoding("gb2312"))
                            , t_book.BookCategory
                            , t_book.BookId
                            , t_book.BookName
                            , t_book.LastUpdateChapterId
                            , SystemUtil.ChangeChapterName( t_book.LastUpdateChapterName)
                            ,t_book.AuthorName
                            , t_book.LastUpdateTime.ToString("HH:ss")
                            ,t_book.SiteId
                            , t_book.IsDownLoad == Consts.ExistChapterContent?"reader":"chapter"
                            );
                    }
                }

                lblLastUpdate.Text = t_sb.ToString();
            }
            catch (Exception ex)
            {
                LogHelper.Error("绑定小说最新更新失败" + ex.ToString());
                RedirectErrorPage();
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(metaKeywords.Content))
            {

                List<string> t_list = m_book_mgr.GetIndexKeyList();

                StringBuilder t_key_sb = new StringBuilder();
                StringBuilder t_meta_sb = new StringBuilder();

                if (t_list != null && t_list.Count > 0)
                {
                    for (int index = 0; index < t_list.Count; index++)
                    {
                        if (index < 10)
                        {
                            t_key_sb.AppendFormat("{0},", t_list[index]);
                        }
                        else
                        {
                            t_meta_sb.AppendFormat("{0},", t_list[index]);
                        }

                    }
                }

                t_key_sb.Append("小说,体验码小说");
                t_meta_sb.Append("小说，体验码小说");

                metaKeywords.Content = t_key_sb.ToString();
                metaDescription.Content = t_meta_sb.ToString();
            }
        }
       
    }
}
